package com.ws.ssmzongjie3.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean= new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
//        factoryBean.setTypeAliasesPackage("com.ws.ssmzongjie3");
        //设置别名
//        org.apache.ibatis.session.Configuration configuration =new org.apache.ibatis.session.Configuration();
//        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);//设置日志输出
//        configuration.setMapUnderscoreToCamelCase(true);//转驼峰
//        factoryBean.setConfiguration(configuration);
        factoryBean.setTypeAliasesPackage("com.ws.ssmzongjie3.bean");//设置别名 和数据实体类所对应的包
        PageInterceptor pageInterceptor=new PageInterceptor();//分页插件
        Properties properties=new Properties();//设置分页参数
        properties.setProperty("helperDialect","mysql");//设置语言未sql
        properties.setProperty("reasonable","true");//设置是否支持页码<=0或者页码>总页数
        pageInterceptor.setProperties(properties);//设置分页参数
        factoryBean.setPlugins(new Interceptor[] {pageInterceptor});//使用分页插件
        return factoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc =new MapperScannerConfigurer();
        msc.setBasePackage("com.ws.ssmzongjie3.mapper");
        return msc;
    }

}
